package com.cn.jx.configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cn.jx.manager.service.query.ISystemInfoServiceQry;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月2日
 * <p>功能描述：CommonInterceptor通用拦截器
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Component
public class CommonInterceptor implements HandlerInterceptor {

	/**
	 * 定义{@link ISystemInfoService systemInfoService <tt>系统接口</tt>}
	 */
	@Autowired
	protected ISystemInfoServiceQry systemInfoServiceQry;
	
	/**
	 * <p>程序掉公之前执行
	 * <p>设置一些页面上需要的属性 获取系统配置参数。系统的logo宽度 标题 名称版本等
	 * <p>base通用路径
	 * <p>baseStatic 静态资源路径
	 * <p>baseTemplate 页面路径
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String path = request.getContextPath();
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String basePath = scheme + "://" + serverName + ":" + port + path;
        request.getSession().setAttribute("base", basePath);
        request.getSession().setAttribute("baseStatic", basePath+"/static");
        request.getSession().setAttribute("baseTemplate", basePath+"/templates");
        
        request.getSession().setAttribute("user_path", basePath+"/admin/uoj/user");
        request.getSession().setAttribute("org_path", basePath+"/admin/uoj/org");
        request.getSession().setAttribute("job_path", basePath+"/admin/uoj/job");
        request.getSession().setAttribute("role_path", basePath+"/admin/permissions/role");
        request.getSession().setAttribute("res_path", basePath+"/admin/permissions/resource");
        request.getSession().setAttribute("dic_path", basePath+"/admin/dictcity");
        request.getSession().setAttribute("alysis_path", basePath+"/admin/analysis");
        request.getSession().setAttribute("doc_path", basePath+"/document");
        request.getSession().setAttribute("file_path", basePath+"/admin/file");
        request.getSession().setAttribute("sys_path", basePath+"/admin/system");
        request.getSession().setAttribute("offic_path", basePath+"/admin/office");
        
        return true;
	}

	/**
	 * <p>发生错误后的一些错误页面
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		int statecode=response.getStatus();
        switch (statecode) {
		case 403: //禁止
			modelAndView.setViewName("/error/403");
			break;
		case 404: //找不到文件
			modelAndView.setViewName("/error/404");
			break;
		case 405: //方法禁用
			modelAndView.setViewName("/error/405");
			break;
		case 500: //服务器内部错误
			modelAndView.setViewName("/error/500");
			break;
		case 503: //服务不可用
			modelAndView.setViewName("/error/503");
			break;
		default:
			break;
		}
	}

	/**
	 * <p>程序完成后执行
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
